/*
 * 文件名：LocalTemporaryCheckServiceImpl.java	 
 * 时     间：上午11:27:50
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.local.dao.mapers.LocalParkSpaceCheckMapper;
import com.local.manager.locks.GlobalStaticEntity;
import com.local.model.LocalParkSpaceCheck;
import com.local.model.LocalParkSpaceCheckExample;
import com.local.model.LocalTemporaryCheck;
import com.local.service.face.LocalParkSpaceCheckService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.entity.PageInfo;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;

/**
 * 
 * 类描述：
 * 
 * @ClassName: LocalParkSpaceCheckServiceImpl
 * @author Administrator
 * @date 2016年4月20日 上午11:21:34
 */
@Service
public class LocalParkSpaceCheckServiceImpl
		extends
		BaseServiceImpl<LocalParkSpaceCheckExample, LocalParkSpaceCheck, String>
		implements LocalParkSpaceCheckService
{
	
	@Autowired
	private LocalParkSpaceCheckMapper localParkSpaceCheckMapper;
	
	@Override
	public BaseMapper<LocalParkSpaceCheckExample, LocalParkSpaceCheck, String> getMapper()
	{
		// TODO Auto-generated method stub
		return localParkSpaceCheckMapper;
	}
	
	@Override
	public LocalParkSpaceCheck queryLocalParkSpaceCheck()
	{
		LocalParkSpaceCheck localParkSpaceCheck = localParkSpaceCheckMapper
				.queryLocalParkSpaceCheck();
		if (Tools.isEmpty(localParkSpaceCheck))
		{
			return null;
		} else
		{
			return localParkSpaceCheck;
		}
	}
	
	@Override
	public Boolean queryLocalParkSpaceCheckStatus()
	{
		if (!Tools.isEmpty(GlobalStaticEntity.instance()
				.getLocalParkSpaceCheck()))
		{
			return GlobalStaticEntity.instance().getLocalParkSpaceCheck();
		} else
		{
			LocalParkSpaceCheck localParkSpaceCheck = localParkSpaceCheckMapper
					.queryLocalParkSpaceCheck();
			if (Tools.isEmpty(localParkSpaceCheck))
			{
				return true;
			} else if ("2".equals(localParkSpaceCheck.getType()))
			{
				return false;
			} else
			{
				return true;
			}
		}
	}
	
	@Override
	public String saveOrUpdateLocalParkSpaceCheck(
			LocalParkSpaceCheck localTemporaryCheck)
	{
		// TODO Auto-generated method stub
		try
		{
			List<LocalParkSpaceCheck> list = localParkSpaceCheckMapper
					.queryByPage(new PageInfo<LocalParkSpaceCheck>());
			if("1".equals(localTemporaryCheck.getType())){
				GlobalStaticEntity.instance().setLocalParkSpaceCheck(true);
			}else{
				GlobalStaticEntity.instance().setLocalParkSpaceCheck(false);
			}
			if (!Tools.isEmpty(list) && !list.isEmpty())
			{
				list.get(0).setType(localTemporaryCheck.getType());
				localParkSpaceCheckMapper.updateByPrimaryKey(list.get(0));
				return "1";
			}
			
			LocalParkSpaceCheck info = new LocalParkSpaceCheck();
			info.setUuid(UUIDGenerateUtil.generate());
			info.setType(localTemporaryCheck.getType());
			localParkSpaceCheckMapper.insertSelective(info);
			return "2";
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
	}
	
}
